﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace webbit
{
    public partial class IntroScreen : PhoneApplicationPage
    {

        WebClient pu;

        IsolatedStorageSettings ISSetting = IsolatedStorageSettings.ApplicationSettings;

        public string URL { get; set; }

        public IntroScreen()
        {
            InitializeComponent();

            Initialazation();
        }

        void Initialazation()
        {
            

            if (IsolatedStorageSettings.ApplicationSettings.Contains("DataKey2"))
            {
                string datakey = IsolatedStorageSettings.ApplicationSettings["DataKey2"] as string;

                if (datakey.Contains("autodownloadbackground"))
                {
                    TS1.IsChecked = true;

                    pu = new WebClient();

                    string pp = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1";
                    pu.DownloadStringAsync(new Uri(pp));
                    pu.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pun_DownloadStringCompleted);
                }
                else
                {
                    TS1.IsChecked = false;
                }
            }
            else
            {
                ISSetting.Add("DataKey2", "autodownloadbackground");
                TS1.IsChecked = true;

                pu = new WebClient();

                string pp = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1";
                pu.DownloadStringAsync(new Uri(pp));
                pu.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pun_DownloadStringCompleted);
            }
        }

		void pun_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null && !e.Cancelled)
            {
                XElement resultElements = XElement.Parse(e.Result);
                    URL = resultElements.Element("image").Element("url").Value;
                    string fileName = URL;
                    System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://www.bing.com" + fileName, UriKind.Absolute));
                    ImageBrush brush = new ImageBrush();
                    brush.Stretch = Stretch.UniformToFill;
                    brush.ImageSource = image;
                    LayoutRoot.Background = brush;
                    ContentPanel.Background = brush;
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string url = string.Format("/BrowserPage.xaml?SearchBoxText={0}", textBox1.Text);
                NavigationService.Navigate(new Uri(url, UriKind.Relative));
            }
        }

        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            rec1.Visibility = Visibility.Visible;
        }

        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            rec1.Visibility = Visibility.Collapsed;
        }

        private void ts1unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                ISSetting["DataKey2"] = "no";
            }
            catch { }
        }

        private void ts1checked(object sender, RoutedEventArgs e)
        {
            try
            {
                ISSetting["DataKey2"] = "autodownloadbackground";
            }
            catch { }
        }
    }
}
